﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Home.Haushalt.Lib;

namespace Home.Haushalt.Gui
{
    /// <summary>
    /// 
    /// </summary>
    public partial class UcUserSelector : UserControl
    {
        /// <summary></summary>
        public event EventHandler OnSelected;

        /// <summary></summary>
        private SettingsCollection _settingsCollection;

        /// <summary>
        /// 
        /// </summary>
        public UcUserSelector()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="settingsCollection"></param>
        public void DataBind(SettingsCollection settingsCollection)
        {
            _settingsCollection = settingsCollection;

            if (_settingsCollection != null && _settingsCollection.Items != null)
            {
                _settingsCollection.Items.ForEach(item => this.Controls.Add(new UcUserSelectorItem(item, UcUserSelectorItem_OnClick)));

                int formCenter=this.Left+this.Width/2;
                int userWidth = this.Controls[0].Width;
                this.Width = settingsCollection.Items.Count * userWidth+50;
                this.Left = formCenter - this.Width/2;

                int segmentWidth = this.Width / settingsCollection.Items.Count;

                for (int i = 0; i < settingsCollection.Items.Count; i++)
                {
                    this.Controls[i].Left = (int)(segmentWidth * (i + 0.5) - (userWidth / 2));
                }

                //SetUser(settingsCollection.Items[0].UserName);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UcUserSelectorItem_OnClick(object sender, EventArgs e)
        {
            SetUser(((UcUserSelectorItem)sender).UserName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userName"></param>
        public void SetUser(string userName)
        {
            _settingsCollection.SetUser(userName);

            UnselectAll();

            GetUserControl(userName).Selected = true;

            if (OnSelected != null)
                OnSelected(this, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        private UcUserSelectorItem GetUserControl(string userName)
        {
            return this.Controls.Cast<UcUserSelectorItem>().Where(item => item.UserName.Equals(userName)).FirstOrDefault();
        }

        /// <summary>
        /// 
        /// </summary>
        private void UnselectAll()
        {
            this.Controls.Cast<UcUserSelectorItem>().ToList().ForEach(item => item.Selected = false);

        }
    }
}
